
$(document).ready(function () {
    var form=$('#xh_form');
    var List = [];
    var url='';
    var FileUrl='';
    var clickFlag=false;

    var praseFrom=new xhForm($('#xh_form'));
    praseFrom.parse();
    var iPCId=addressUri.getParam('iPCId');
    var iUserId=addressUri.getParam('iUserId');

    $("#fileInput").fileinput('destroy');
    fileInput.initFileInput('fileInput',baseUrl+'Tch/ajaxUpload?iUserId='+iUserId);
    var count=0; //记录上传次数
    $("#fileInput").on("fileuploaded", function (event, data, previewId, index) {
        count++;
        List.push({ FileName: data.response.fileName, KeyID: previewId,url:data.response.url })
        if(clickFlag&&(count==data.files.length)){
            clickFlag=false;
            count=0;
            save();
        }
    });

    $("#fileInput").on("filesuccessremove", function (event, data, previewId, index) {
        for (var i = 0; i < List.length; i++) {
            if (List[i].KeyID== data) {
                List.splice(i,1);
            }
        }
    });


    if(iPCId!=null){
        praseFrom.fill(trackInfo);
        if(trackInfo['sFiles']!=null){
            FileUrl += trackInfo['sFiles'];
        }
        if(trackInfo['sImages']!=null){
            url   += trackInfo['sImages'];
        }
    }


    praseFrom.bindRequired($('#bt_modal_confirm',parent.document));
    // //验证


    $('#bt_modal_confirm',parent.document).click(function () {
        if ($('[class=file-preview-thumbnails] [id^=preview] ')[0]!=undefined){//有文件且文件未上传
            var isUploaded=true;
            $('[class=file-preview-thumbnails] [id^=preview]').each(function () {
                if(!$(this).hasClass('file-preview-success')&&!$(this).hasClass('file-preview-error')){
                    isUploaded=false;
                }
            });
            if(isUploaded){//文件已上传
                save();
            }else {
                clickFlag=true;
                $("#fileInput").fileinput('upload');
            }
        }else {
            save();
        }

    });

    function save() {
        for (var i = 0; i < List.length; i++) {
            var footerfix= List[i]['url'].split('.');
            footerfix = footerfix[footerfix.length-1];
            footerfix=footerfix.toLowerCase();
            if(footerfix=='jpg'||footerfix=='gif'||footerfix=='png') {  //这里的url只指图片
                url += List[i]['url'] + ',';
            }else {
                FileUrl+=List[i]['url']+',';
            }
        }
        $('#sImages').val(url);
        $('#sFiles').val(FileUrl);
        //var timeWraper=$('#timeWraper',window.top.xhTop.getActiveFrame().frames['operationGradeTrace'].document);
        $.ajax({
            url : baseUrl+'tch/saveFeedback',
            data:$("#xh_form").serialize(),
            dataType:'json',
            type:'post',
            success:function (data) {
                if(data=='fail'){
                    alert('内容重复');
                    return false;
                }
            },
            error:function (result) {
                console.log(result);
                window.top.swal('',result['responseText'],'error');
            },
            complete:function () {
                if(window.top.xhTop.getActiveFrame()['publicClass']!=null){
                    window.top.xhTop.getActiveFrame()['publicClass'].classList.dataGridReload();
                }else {
                    window.top.xhTop.getActiveFrame().classList.dataGridReload();
                }
                
                window.top.xhTop.closeModal();
                //清空
                url='';
                FileUrl='';
                List=[];
            }
        });


    }

});
